package com.beauxi.activities;



import java.util.Random;

import com.beauxi.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MosaicoActivity extends Activity {
    
    protected static final String KEY_ROWID = "_id";
    private Integer[] mThumbIds;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mosaico);
        
        mThumbIds = loadImagenes();
        
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new MosaicoImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(MosaicoActivity.this, "" + position + " id: " + id, Toast.LENGTH_SHORT).show();
                Intent i = new Intent(MosaicoActivity.this,CromoDetalleActivity.class);
                ImageView iv = (ImageView) v;
            	i.putExtra(KEY_ROWID, iv.getId());
            	startActivity(i);
            }
        });
    }
	
	 private Integer[] loadImagenes() {
			Integer[] res = new Integer[231];
			Random rnd = new Random();
			int r = 0;
			for(int i=0;i<231;i++){
				r = rnd.nextInt(3);
				res[i] = new Integer(r==0?R.drawable.beauxi_1:r==1?R.drawable.beauxi_2:R.drawable.beauxi_3);
			}
			return res;
		}
	
	public class MosaicoImageAdapter extends BaseAdapter {
	    private Context mContext;
	    
	    // references to our images
	   
	    
	    
	    public MosaicoImageAdapter(Context c) {
	        mContext = c;
	        
	    }

	   

		public int getCount() {
	        return mThumbIds.length;
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView imageView;
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	            imageView = new ImageView(mContext);
	            //imageView.setLayoutParams(new GridView.LayoutParams(60, 90));
	            imageView.setAdjustViewBounds(true);
	            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
	            imageView.setPadding(1, 1, 1, 1);
	        } else {
	            imageView = (ImageView) convertView;
	        }

	        imageView.setImageResource(mThumbIds[position]);
	        imageView.setId(mThumbIds[position]);
	        return imageView;
	    }

	}
}